package edu.nf.relaxenjoy.service.impl;

import com.github.pagehelper.PageInfo;
import edu.nf.relaxenjoy.common.util.ConvertUtils;
import edu.nf.relaxenjoy.common.util.TreeUtils;
import edu.nf.relaxenjoy.domain.entity.Category;
import edu.nf.relaxenjoy.domain.entity.Post;
import edu.nf.relaxenjoy.domain.entity.User;
import edu.nf.relaxenjoy.domain.vo.CategoryVo;
import edu.nf.relaxenjoy.domain.vo.PostVo;
import edu.nf.relaxenjoy.mapper.CategoryMapper;
import edu.nf.relaxenjoy.mapper.PostMapper;
import edu.nf.relaxenjoy.mapper.UserMapper;
import edu.nf.relaxenjoy.service.CategoryService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @author zhu
 * @date 2024-12-19
 */
@Service
@RequiredArgsConstructor
public class CategoryServiceImpl implements CategoryService {

    private final CategoryMapper categoryMapper;

    private final PostMapper postMapper;

    private final UserMapper userMapper;

    @Override
    public PageInfo<PostVo> getPostListByCategory(long page, long limit, String id, Integer type) {
        PageInfo<PostVo> pages = new PageInfo<>();
        PageInfo<Post> postPageInfo;
        if (type == 1) {
            //随便看看
            postPageInfo = new PageInfo<>(postMapper.getListByCategoryPid(page, limit, Long.valueOf(id)));
        } else if (type == 2) {
            //获取热门
            postPageInfo = new PageInfo<>(postMapper.getPageByCategoryPid(page, limit, Long.valueOf(id)));
        } else {
            //获取二级分类下的所有笔记
            postPageInfo = new PageInfo<>(postMapper.getPageByCategoryId(page, limit, Long.valueOf(id)));
        }

        List<Post> postList = postPageInfo.getList();
        List<Long> uids = new ArrayList<>();
        postList.forEach(item -> {
            uids.add(item.getUserId());
        });

        List<User> userList = userMapper.listByIds(uids);
        HashMap<Long, User> userMap = new HashMap<>();
        userList.forEach(item -> {
            userMap.put(item.getUserId(), item);
        });

        List<PostVo> res = new ArrayList<>();
        PostVo postVo;
        User user;
        for (Post model : postPageInfo.getList()) {
            postVo = ConvertUtils.sourceToTarget(model, PostVo.class);
            user = userMap.get(model.getUserId());
            postVo.setUserId(user.getUserId());
            postVo.setUsername(user.getUsername());
            postVo.setAvatar(user.getAvatar());
            res.add(postVo);
        }
        pages.setList(res);
        pages.setTotal(postPageInfo.getTotal());
        pages.setIsFirstPage(postPageInfo.isIsFirstPage());
        pages.setIsLastPage(postPageInfo.isIsLastPage());
        pages.setPageNum(postPageInfo.getPageNum());
        pages.setPageSize(postPageInfo.getPageSize());

        return pages;
    }

    @Override
    public List<CategoryVo> getTreeCategory() {
        List<Category> categoryList = categoryMapper.getList();
        List<CategoryVo> voList = ConvertUtils.sourceToTarget(categoryList, CategoryVo.class);
        return TreeUtils.build(voList);
    }
}
